// packageC/pages/package/agv_manage/lineManage/addLine/addLine.js
const mybehavior = require("../../../../../../behaviors/requests")
import WxValidate from "../../../../../../Wx_Validate/WxValidate"
Page({

  /**
   * 页面的初始数据
   */
  behaviors: [mybehavior],
  data: {
    baseUrl: "https://logisticsengineering.gree.com/agv_dispatch_system",
    tag_id: "",
    department: "",
    workshop: "",
    location: "",
    material_name: "",
    special_agv_info: [],
    alone_column_flag: "",
    key: "",
    options_workshop: [],
    options_line: [],
    options_special_agv_info: [],
    options_alone_column_flag: ["是", "否"],

    caozuo_show: false,

    control_val: "",
    cz_id: "",
    destination_station: [],
    options_destination_station: [],
    add_tag_show: true
  },
  // 扫描
  scan1(e) {
    let key = e.currentTarget.dataset.key
    console.log(key);
    const self = this;
    wx.scanCode().then((res) => {
      const label_code = res.result;
      wx.showLoading({
        title: "loading"
      })
      console.log(label_code);
      setTimeout(function () {
        wx.hideLoading()
        self.setData({
          [key]: label_code,
        })
      }, 500)

    }).catch(err => {
      console.log(err);
    });
  },
  // 新增岗位数据
  submit() {
    let url = this.data.baseUrl + "/add_post_station"
    let lists = []
    if (this.data.special_agv_info.length != 0) {
      for (let i = 0; i < this.data.special_agv_info.length; i++) {
        lists.push(this.data.special_agv_info[i].agv_num)
      }
    }

    let data = {
      department: this.data.department,
      workshop: this.data.workshop,
      location: this.data.location,
      material_name: this.data.material_name,
      tag_id: this.data.tag_id,
      alone_column_flag: this.data.alone_column_flag === '是' ? true : false,
      destination_station: this.data.destination_station,
      special_agv_list: lists,
      post_id: this.data.key,
    }
    if (!this.WxValidate.checkForm(data)) {
      const error = this.WxValidate.errorList[0]
      wx.showToast({
        title: error.msg,
        icon: 'none'
      })
    } else {
      this.post(url, data).then(res => {
        if (res.code == 20000) {
          wx.showModal({
            title: res.msg,
            content: '',
            showCancel: false,
            complete: (res) => {

              if (res.confirm) {
                wx.navigateBack()

              }
            }
          })


        } else {

          wx.showToast({
            title: res.msg,
            icon: 'none'
          })
        }
      }).catch(err => {

        console.log(err);
        wx.showToast({
          title: err.msg,
          icon: 'none'
        })
      })
    }


  },
  //初始化表单验证规则
  initValidate() {
    let rules = {
      department: {
        required: true,
      },
      workshop: {
        required: true,
      },

      location: {
        required: true,
      },

      material_name: {
        required: true,
      },
      tag_id: {
        required: true,
      },
      alone_column_flag: {
        required: true,
      },
      destination_station: {
        required: true,
      },
      special_agv_list: {
        required: true,
      },






    }

    let message = {
      department: {
        required: "部门-不能为空",
      },
      tag_id: {
        required: "标签-不能为空",
      },
      workshop: {
        required: "车间-不能为空",
      },
      location: {
        required: "线体-不能为空",
      },
      material_name: {
        required: "岗位名称-不能为空",
      },
      alone_column_flag: {
        required: "是否单列配送-不能为空",
      },
      destination_station: {
        required: "停靠站点-不能为空",
      },
      special_agv_list: {
        required: "配送车辆-不能为空",
      },





    }
    //实例化当前的验证规则和提示消息
    this.WxValidate = new WxValidate(rules, message);
  },
  // 下拉选择
  getPickerVlaue1(e) {
    let key = e.target.id
    this.setData({
      [key]: e.detail
    })
    if (key == "department") {
      this.getAgvNameData()
      this.getWorkshopData()
    }
    if (key == "workshop") {
      this.getLineData()

    }
  },
  // 停靠站点弹窗
  onClick() {
    let lists = JSON.parse(JSON.stringify(this.data.destination_station))
    this.setData({
      caozuo_show: true,
      options_destination_station: lists

    })
  },
  // 停靠站点-弹窗添加按钮是否可选
  chechAddTagBtn() {
    if (this.data.control_val != "") {
      this.setData({
        add_tag_show: false
      })
    } else {
      this.setData({
        add_tag_show: true
      })
    }
  },
  // 添加-停靠站点-值
  addTag() {
    let lists = JSON.parse(JSON.stringify(this.data.options_destination_station))
    if (lists.includes(Number(this.data.control_val))) {
      wx.showToast({
        title: '禁止重复添加！',
        icon: 'none'
      })
      this.setData({
        control_val: "",
      })
    } else if (lists.length == 2) {
      wx.showToast({
        title: '最多只允许输入两张卡号！',
        icon: 'none'
      })
      this.setData({
        control_val: "",
      })
    } else {
      lists.push(Number(this.data.control_val))
      this.setData({
        options_destination_station: lists,
        control_val: "",
        add_tag_show: true

      })
    }


  },
  // 删除-停靠站点-值值
  deleteTag(e) {
    // console.log(e.target.dataset.index);
    let lists = JSON.parse(JSON.stringify(this.data.options_destination_station))
    lists.splice(e.target.dataset.index, 1)
    this.setData({
      options_destination_station: lists,
    })
  },
  // 停靠站点-值确认
  onConfirm() {
    let lists = JSON.parse(JSON.stringify(this.data.options_destination_station))
    this.setData({
      destination_station: lists,
      options_destination_station: []
    })
  },
  // 取消-停靠站点-值
  onCancel() {
    this.setData({
      caozuo_show: false,
      options_destination_station: [],
      control_val: "",
      cz_id: "",
      add_tag_show: true
    })
  },
  // 配送车辆-弹窗
  onAgvClick(e) {
    this.setData({
      caozuo_agv_show: true,

    })
  },

  // 删除-配送车辆-值值
  deleteAgvTag(e) {
    // console.log(e.target.dataset.key);
    let lists = this.data.special_agv_info
    for (let i in lists) {
      if (lists[i].agv_name == e.target.dataset.key.agv_name) {
        lists.splice(i, 1)
      }
    }
    this.setData({
      special_agv_info: lists
    })
  },
  // 配送车辆-值确认
  onAgvConfirm(e) {
    // console.log(e.detail.value);
    let lists = this.data.special_agv_info
    if (lists.includes(e.detail.value)) {
      wx.showToast({
        title: '禁止重复添加！',
        icon: 'none'
      })

    } else {
      lists.push(e.detail.value)
      this.setData({
        special_agv_info: lists,
        caozuo_agv_show: false

      })
    }

  },
  // 取消-配送车辆-值
  onAgvCancel() {
    this.setData({
      caozuo_agv_show: false,
    })
  },
  // 查询部门
  getDepartmentData() {
    let url = this.data.baseUrl + "/get_department_list"

    let data = {
    }
    // console.log("查询部门-传过去的数据", data);
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options1: res.data

        })

      } else {
        this.setData({
          options1: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options1: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询车间数据
  getWorkshopData() {
    let url = this.data.baseUrl + "/get_workshop_list"

    let data = {
      department: this.data.department
    }
    // console.log("查询车间数据-传过去的数据", data);
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options_workshop: res.data

        })

      } else {
        this.setData({
          options_workshop: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options_workshop: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询线体数据
  getLineData() {
    let url = this.data.baseUrl + "/get_line_list"

    let data = {
      department: this.data.department,
      workshop: this.data.workshop,
    }
    // console.log("查询线体数据-传过去的数据", data);
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options_line: res.data

        })

      } else {
        this.setData({
          options_line: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options_line: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },
  // 查询车辆数据
  getAgvNameData() {
    let url = this.data.baseUrl + "/get_agv_name_map"

    let data = {
      department: this.data.department,
    }
    // console.log("查询车辆数据-传过去的数据", data);
    this.get(url, data).then(res => {
      if (res.code == 20000) {
        this.setData({
          options_special_agv_info: res.data

        })

      } else {
        this.setData({
          options_special_agv_info: [],
        })
        wx.showToast({
          title: res.msg,
          icon: 'error'
        })
      }
    }).catch(err => {
      this.setData({
        options_special_agv_info: [],
      })
      console.log(err);
      wx.showToast({
        title: err.msg,
        icon: 'none'
      })
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.initValidate()
    this.getDepartmentData()


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})